package cn.bo.cloud.common.core.result;


import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.http.HttpStatus;

import java.io.Serializable;

/**
 * @ClassName ResultResponce
 * @Description TODO
 * @Author jb.zhou
 * @Date 2020/2/28
 * @Version 1.0
 */
public class ResponceResult<T> implements Serializable {

    /**
     * 响应状态码
     */
    private int code;

    /**
     * 响应数据
     */
    private T content;
    /**
     * 信息
     */

    private String message;

    public static <T> ResponceResult<T> of(){
        return new ResponceResult<>();
    }

    public ResponceResult set(int code, T content) {
        this.code = code;
        this.content = content;
        return this;
    }

    public ResponceResult set(int code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public ResponceResult set(int code, String message, T content) {
        this.set(code, content);
        this.message = message;
        return this;
    }

    /**
     * 构造方法
     */
    public ResponceResult() {
    }

    /**
     * 静态方法
     * @return
     */
    public static ResponceResult success() {
        return success(HttpStatus.SC_OK,"SUCCESS");
    }
    public static  <T> ResponceResult<T> success(T content) {
        return success(HttpStatus.SC_OK,content);
    }
    public static <T> ResponceResult<T> success(int code, T content) {
        return ResponceResult.of().set(code, content);
    }
    public static <T> ResponceResult<T> success(int code, String message, T content) {
        return ResponceResult.of().set(code, message, content);
    }


    public static ResponceResult failure() {
        return failure(HttpStatus.SC_INTERNAL_SERVER_ERROR,"ERROR");
    }

    public static <T> ResponceResult<T> failure(T content) {
        return failure(HttpStatus.SC_INTERNAL_SERVER_ERROR, "ERROR", content);
    }
    public static ResponceResult failure(int code, String message) {
        return ResponceResult.of().set(code, message);
    }
    public static <T> ResponceResult<T> failure(int code, T content) {
        return failure(code, "ERROR", content);
    }
    public static <T> ResponceResult<T> failure(int code, String message, T content) {
        return ResponceResult.of().set(code, message, content);
    }

    public boolean isOk() {
        return this.getCode() == HttpStatus.SC_OK;
    }

    public int getCode() {
        return code;
    }

    public ResponceResult setCode(int code) {
        this.code = code;
        return this;
    }

    public T getContent() {
        return content;
    }

    public ResponceResult setContent(T content) {
        this.content = content == null ? null : content;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ResponceResult setMessage(String message) {
        this.message = message;
        return this;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
                .append("code", code)
                .append("message", message)
                .append("content", content)
                .toString();
    }
    public String toJson(){
        return new ToStringBuilder(ToStringStyle.JSON_STYLE)
                .append("code", code)
                .append("message", message)
                .append("content", content)
                .toString();
    }
}

